package filters;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

public class AuthGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        if (isExclude(request.getPath().toString())) {
            return chain.filter(exchange);
        }
        //token解析失败的时候try拦截并返回401枚举
        ServerHttpResponse response = exchange.getResponse();
        //response.setStatusCode(HttpStatus.UNAUTHORIZED);
        //return response.setComplete();
        //long userId = 1L;
        //String userInfo = Long.toString(userId);
        //ServerWebExchange build = exchange.mutate()
        //        .request(b -> b.header("user-info", userInfo))
        //        .build();
        //return chain.filter(build);

        return chain.filter(exchange);
    }


    //返回零 是过滤器中第一个经过的
    @Override
    public int getOrder() {
        return 0;
    }

    //等实现
    private boolean isExclude(String string) {
        return true;
        //            return false
    }
}
